#include <clock/lcd_clock.h>

#include <arduino/Arduino.h>


namespace clock
{


lcd_clock::lcd_clock(lcd::abstract_lcd* screen, int line, int col)
  : _lcd(screen)
  , _line(line)
  , _col_hour(col), _col_min(col + 3), _col_sec(col + 6)
  , _blink{false, false, false}
  , _blink_status{false, false, false}
{
  _start_time.second = 0;
  _start_time.minute = 0;
  _start_time.hour   = 0;
}


void lcd_clock::update(unsigned long delta)
{
  _current_time = _start_time + delta/1000;

  if (!_blink[0] || _blink_status[0])
  {
    if (_current_time.hour < 10)
    {
      _lcd->print(_line, _col_hour    , 0);
      _lcd->print(_line, _col_hour + 1, _current_time.hour);
    }
    else
      _lcd->print(_line, _col_hour, _current_time.hour);
  }
  else _lcd->print(_line, _col_hour, "  ");

  if (!_blink[1] || _blink_status[1])
  {
    if (_current_time.minute < 10)
    {
      _lcd->print(_line, _col_min    , 0);
      _lcd->print(_line, _col_min + 1, _current_time.minute);
    }
    else
      _lcd->print(_line, _col_min, _current_time.minute);
  }
  else _lcd->print(_line, _col_min, "  ");

  if (!_blink[2] || _blink_status[2])
  {
    if (_current_time.second < 10)
    {
      _lcd->print(_line, _col_sec    , 0);
      _lcd->print(_line, _col_sec + 1, _current_time.second);
    }
    else
      _lcd->print(_line, _col_sec, _current_time.second);
  }
  else _lcd->print(_line, _col_sec, "  ");

  _lcd->print(_line, _col_min - 1, ":");
  _lcd->print(_line, _col_sec - 1, ":");

  _blink_status[0] = !_blink_status[0];
  _blink_status[1] = !_blink_status[1];
  _blink_status[2] = !_blink_status[2];
}


void lcd_clock::blink_hour(bool status)
{ _blink[0] = status; }


void lcd_clock::blink_minute(bool status)
{ _blink[1] = status; }


void lcd_clock::blink_second(bool status)
{ _blink[2] = status; }


} // clock
